package com.zjlanyun.erp.interior.warehouse;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.yolanda.nohttp.rest.Response;
import com.zjlanyun.erp.R;
import com.zjlanyun.erp.network.WebApi;
import com.zjlanyun.erp.nohttp.HttpRequest;
import com.zjlanyun.erp.nohttp.SimpleHttpListener;
import com.zjlanyun.erp.view.BaseActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;

public class SaleOutBill extends BaseActivity {

    @Bind(R.id.lv) ListView lv;

    private ArrayList<HashMap<String, Object>> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sale_out_bill);
        initBaseView();
        initView();
        initData();
    }

    private void initView(){
        title.setText("销售出库单");
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, SaleOutBind.class);
                intent.putExtra("FEntryID", (Integer) mList.get(position).get("FEntryID"));
                startActivityForResult(intent, 1);
            }
        });
    }

    private void initData(){
        mList = new ArrayList<>();
        HttpRequest httpRequest = new HttpRequest(mContext, WebApi.production(), "getSaleOutBill", true);
        httpRequest.send(new SimpleHttpListener<JSONObject>() {
            @Override
            public void onSucceed(int what, Response<JSONObject> response) {
                super.onSucceed(what, response);
                try {
                    JSONArray jsonArray = response.get().getJSONObject("data").getJSONArray("list");
                    for (int i = 0; i < jsonArray.length(); i++){
                        JSONObject item = jsonArray.getJSONObject(i);
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        map.put("FBillNO", "发货单号："+item.getString("FBillNO"));
                        map.put("FCustName", "客户："+item.getString("FCustName"));
                        map.put("FNumber", "产品代码："+item.getString("FNumber"));
                        map.put("FName", "产品名称："+item.getString("FName"));
                        map.put("FDate", "日期："+item.getString("FDate").substring(0, 10));
                        map.put("FEntryID", item.getInt("FEntryID"));

//                        Iterator it = item.keys();
//                        while (it.hasNext()){
//                            String key = (String) it.next();
//                            String value = item.getString(key);
//                            map.put(key, value);
//                        }
                        mList.add(map);
                    }
                    SimpleAdapter simpleAdapter = new SimpleAdapter(mContext, mList, R.layout.sale_out_bill_item, new String[]{"FBillNO","FCustName","FNumber","FName","FDate"}, new int[]{R.id.tv1, R.id.tv5, R.id.tv2, R.id.tv3, R.id.tv4});
                    lv.setAdapter(simpleAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
            initData();
        }
    }
}
